# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::canvas::GD;
use strict;
use canvas::Generic;
@dcExplorer::canvas::GD::ISA = qw(dcExplorer::canvas::Generic);

use GD::Simple;
use GD::Polyline;
use canvas::Color;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self  = $class->SUPER::new(@_);
	$self->{Image}->bgcolor('black');
	$self->{Image}->clear;
	$self->{Image}->penSize(1);
	$self->{Image}->font('Arial');
	bless ($self, $class);
	return $self;
}

sub color
{
	my $self = shift;
	my $color = shift;
	return defined($color)?$self->{Image}->alphaColor($color->{r}, $color->{g}, $color->{b}, $color->{a}):undef;
}

sub _fgcolor
{
	my $self = shift;
	my $color = shift;
	$self->{Image}->fgcolor($self->color($color));
}

sub _bgcolor
{
	my $self = shift;
	my $color = shift;
	$self->{Image}->bgcolor($self->color($color));
}

sub Circle
{
	my $self = shift;
	my ($x, $y, $r, $fg, $bg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	$self->_bgcolor($bg);
	$self->{Image}->penSize($penSize);
	$self->{Image}->moveTo($x, $y);
	$self->{Image}->ellipse($self->{bsize} * $r / 100, $self->{bsize} * $r / 100);
}

sub Line
{
	my $self = shift;
	my ($x1, $y1, $x2, $y2, $fg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	$self->{Image}->penSize($penSize);
	$self->{Image}->moveTo($x1, $y1);
	$self->{Image}->lineTo($x2, $y2);
}

sub Rectangle
{
	my $self = shift;
	my ($x1, $y1, $x2, $y2, $fg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	$self->_bgcolor(undef);
	$self->{Image}->penSize($penSize);
	$self->{Image}->rectangle($x1, $y1, $x2, $y2);
}

sub Poly
{
	my $self = shift;
	my ($points, $fg, $bg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	$self->_bgcolor(undef);
	$self->{Image}->penSize($penSize);
	my $polygon = new GD::Polyline;
	$polygon->addPt(@{$_}) foreach @{$points};
	$self->{Image}->polygon($polygon);
}

sub Text
{
	my $self = shift;
	my ($x, $y, $jx, $jy, $text, $color, $size) = @_;
	$size = 8 unless defined $size;
	my @b = GD::Image->stringFT($self->color($color), './FreeSans.ttf', $size, 0, 0, 0, $text);
	my ($rx, $ry) = ($x - $b[6] - ($b[2] - $b[6]) * ($jx + 1) / 2, $y - $b[7] - ($b[3] - $b[7]) * ($jy + 1) / 2);
	$self->{Image}->stringFT($self->color($color), './FreeSans.ttf', $size, 0, $rx, $ry, $text);
}

1;
